package io.unicorn.modules.water.controller;

import com.alibaba.fastjson.JSONObject;
import io.unicorn.common.utils.DateUtils;
import io.unicorn.common.utils.ExcelUtils;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;
import io.unicorn.common.validator.ValidatorUtils;
import io.unicorn.modules.water.entity.ErrorReport1Entity;
import io.unicorn.modules.water.entity.ErrorReportEntity;
import io.unicorn.modules.water.service.ErrorReport1Service;
import io.unicorn.modules.water.service.ErrorReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2022-03-05 20:25:19
 */
@Slf4j
@RestController
@RequestMapping("water/errorreport1")
public class ErrorReport1Controller {
    @Autowired
    private ErrorReport1Service errorReport1Service;


    @RequestMapping("/list")
    @RequiresPermissions("water:errorreport:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = errorReport1Service.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{proName}")
    @RequiresPermissions("water:errorreport:info")
    public R info(@PathVariable("proName") String proName){
        ErrorReport1Entity errorReport = errorReport1Service.getById(proName);

        return R.ok().put("errorReport", errorReport);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("water:errorreport:save")
    public R save(@RequestBody ErrorReport1Entity errorReport){
        errorReport1Service.save(errorReport);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("water:errorreport:update")
    public R update(@RequestBody ErrorReport1Entity errorReport){
        ValidatorUtils.validateEntity(errorReport);
        errorReport1Service.updateById(errorReport);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("water:errorreport:delete")
    public R delete(@RequestBody String[] proNames){
        errorReport1Service.removeByIds(Arrays.asList(proNames));

        return R.ok();
    }

    @RequestMapping("/export")
    @RequiresPermissions("water:errorreport:list")
    public void export(@RequestParam Map<String, Object> params,
                       HttpServletRequest request,
                       HttpServletResponse repsonse){
        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));

        PageUtils page = errorReport1Service.queryPage(jsonObject);
        List<?> listEntity = page.getList();

        ExcelUtils.exportXlsx(
                repsonse,
                "export",
                "io.unicorn.modules.water.entity.ErrorReport1Entity",
                listEntity);
    }


    //下载全部导出数据表
    @RequestMapping("/download")
    public void code(@RequestParam Map<String, Object> params,
                     HttpServletResponse response) throws IOException {

        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));
        String deptId = jsonObject.getString("dept");
        byte[] data = null;
        if(StringUtils.isNotBlank(deptId) && StringUtils.isNotEmpty(deptId))
        {
            data = errorReport1Service.generatorExcel(jsonObject);
        }

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"error_"+ DateUtils.format(new Date(),"yyyyMMddHHmmss")+".xlsx\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }


    @RequestMapping("/genDeptErrorReport")
    public R DeptErrorReport(@RequestParam Map<String, Object> params){

        //JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));
        errorReport1Service.GenErrorReportByProName(params);

        return R.ok();
    }

}
